package com.broada.apm.entity.convert;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broada.apm.entity.ApplicationSystem;
import com.broada.apm.entity.IPLocation;
import com.broada.apm.entity.L7Module;
import com.broada.apm.entity.Relation;
import com.broada.apm.entity.Service;
import com.broada.apm.entity.ServiceTopology;
import com.broada.apm.entity.TransactionChannel;
import com.broada.apm.entity.TransactionType;
import com.broada.apm.utils.JsonUtil;

/**
 * 实体类属性复制转换
 * 
 * @author liss Create By 2014年12月24日 下午2:35:51
 */
public class BeanConvert {

	private static Logger log = LoggerFactory.getLogger(BeanConvert.class);

	public static List<ApplicationSystem> convertApplication(List<com.broada.apm.model.ApplicationSystem> list) {
		List<ApplicationSystem> apps = new ArrayList<ApplicationSystem>();
		if (checkObjectIsNull(list)) {
			for (com.broada.apm.model.ApplicationSystem app : list) {
				ApplicationSystem application = (ApplicationSystem) convertOjbect(app);
				apps.add(application);
			}
		}
		return apps;
	}

	public static List<Service> convertService(List<com.broada.apm.model.Service> list) {
		List<Service> services = new ArrayList<Service>();
		if (checkObjectIsNull(list)) {
			for (com.broada.apm.model.Service ser : list) {
				Service service = (Service) convertOjbect(ser);
				services.add(service);
			}
		}
		return services;
	}

	public static List<Relation> convertRelation(List<com.broada.apm.model.Relation> list) {
		List<Relation> relations = new ArrayList<Relation>();
		if (checkObjectIsNull(list)) {
			for (com.broada.apm.model.Relation rel : list) {
				Relation relation = (Relation) convertOjbect(rel);
				relations.add(relation);
			}
		}
		return relations;
	}
	
	public static List<ServiceTopology> convertServiceTopology(List<com.broada.apm.model.ServiceTopology> list) {
		List<ServiceTopology> topology = new ArrayList<ServiceTopology>();
		if (checkObjectIsNull(list)) {
			for (com.broada.apm.model.ServiceTopology logy : list) {
				ServiceTopology relation = (ServiceTopology) convertOjbect(logy);
				topology.add(relation);
			}
		}
		return topology;
	}
	
	public static List<TransactionChannel> convertTransactionChannel(List<com.broada.apm.model.TransactionChannel> list) {
		List<TransactionChannel> transactions = new ArrayList<TransactionChannel>();
		if (checkObjectIsNull(list)) {
			for (com.broada.apm.model.TransactionChannel rel : list) {
				TransactionChannel relation = new TransactionChannel(rel.getAppId(), rel.getChannelCode(), rel.getChannelName());
				transactions.add(relation);
			}
		}
		return transactions;
	}
	
	public static List<TransactionType> convertTransactionType(List<com.broada.apm.model.TransactionType> list) {
		List<TransactionType> transactions = new ArrayList<TransactionType>();
		if (checkObjectIsNull(list)) {
			for (com.broada.apm.model.TransactionType rel : list) {
				TransactionType relation = new TransactionType(rel.getAppId(), rel.getL7SubProto(), rel.getTypeCode(), rel.getTypeName(), rel.getSubTypeCode(), rel.getSubTypeName());
				transactions.add(relation);
			}
		}
		return transactions;
	}

	/**
	 * data对象转front对象
	 * 
	 * @param obj
	 * @return
	 */
	public static Object convertOjbect(Object obj) {
		if (obj instanceof com.broada.apm.model.ApplicationSystem) {
			ApplicationSystem app = new ApplicationSystem();
			copyBean(app, obj);
			app.setNodeIds(((com.broada.apm.model.ApplicationSystem) obj).getServiceIds());
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				map = JsonUtil.json2map(((com.broada.apm.model.ApplicationSystem) obj).getValidTime());
			} catch (Exception e) {
				log.error("转换对象失败：" + e.getMessage());
			}
			Map<String, String> validTime = new HashMap<String, String>();
			for (String key : map.keySet()) {
				validTime.put(key, map.get(key).toString());
			}
			app.setValidTimeMap(validTime);
			return app;
		}
		if (obj instanceof com.broada.apm.model.Service) {
			Service service = new Service();
			copyBean(service, obj);
			service.setFlow(service.getTx() + service.getRx());
			return service;
		}
		if (obj instanceof com.broada.apm.model.Relation) {
			Relation relation = new Relation();
			copyBean(relation, obj);
			relation.setRelationId(((com.broada.apm.model.Relation) obj).getId());
			return relation;
		}
		if (obj instanceof com.broada.apm.model.L7Module) {
			L7Module module = new L7Module();
			copyBean(module, obj);
			return module;
		}
		if (obj instanceof com.broada.apm.model.ServiceTopology) {
			ServiceTopology module = new ServiceTopology();
			copyBean(module, obj);
			return module;
		}
		return null;
	}

	public static void copyBean(Object toBean, Object fromBean) {
		try {
			// 自定义一个转换器
			ConvertUtils.register(new Converter() {
				@Override
				public Object convert(Class type, Object value) {
					if (value == null) {
						return value;
					}
					if (value.getClass() == Date.class) {
						return value;
					}
					return value;
				}
			}, Date.class);
			BeanUtils.copyProperties(toBean, fromBean);
		} catch (Exception e) {
			log.error("实体类转换失败", e);
		}
	}

	public static boolean checkObjectIsNull(Object obj) {
		if (obj == null) {
			return false;
		}
		return true;
	}

	/**
	 * front对象转data对象
	 * 
	 * @param obj
	 * @return
	 */
	public static Object convertFrontObjToDataObj(Object obj) {
		if (obj instanceof ApplicationSystem) {
			com.broada.apm.model.ApplicationSystem app = new com.broada.apm.model.ApplicationSystem();
			copyBean(app, obj);
			app.setId(((ApplicationSystem) obj).getId());
			return app;
		}
		if (obj instanceof Service) {
			com.broada.apm.model.Service service = new com.broada.apm.model.Service();
			copyBean(service, obj);
			service.setId(((Service) obj).getId());
			return service;
		}
		if (obj instanceof Relation) {
			com.broada.apm.model.Relation relation = new com.broada.apm.model.Relation();
			copyBean(relation, obj);
			relation.setId(((Relation) obj).getId());
			return relation;
		}
		if (obj instanceof IPLocation) {
			com.broada.apm.model.IPLocation ipLocation = new com.broada.apm.model.IPLocation();
			copyBean(ipLocation, obj);
			ipLocation.setId(((IPLocation) obj).getId());
			return ipLocation;
		}
		return null;
	}
}
